Uec3.permissionOperationModule = Ext.extend(Ext.app.Module, {
    id: 'permission_operation',
    objsCollection:new Ext.util.MixedCollection(true),
    getObjByKey:function(key){
		return this.objsCollection.get(key);
	},

    initPanel:function(params) {
   	var module=this;
   	var nodes=[];
   	var parentNodes=[];
   	var ajax=Uec3.App.Uec3Ajax;
	var tree = new Ext.tree.TreePanel({
        height: 400,
       	width: 300,
        useArrows:true,
        autoScroll:true,
        enableDD: true,
        layout:'form',
        //rootVisible: false,
        frame: true,
        root: {
            id: 0,
            text: '权限角色',
            permission:{id:0,isGroup:1},
            nodeType: 'async'
        },
        loader: new Ext.tree.TreeLoader({
            dataUrl:Uec3.App.Constants.appUrl+'/permission/base!getPermissionList.do',
            listeners:{
            	'loadexception':function(tl,node,response){
            		Ext.Msg.alert("错误","加载失败,请重新加载!");
            	}
            }
        }),
        tbar: [{
			text: "添加权限",
			iconCls:'icon-add',
			handler: function(){
			var curNode=tree.getSelectionModel().getSelectedNode();
				if(curNode){
					form.getForm().reset();
					form.getForm().findField("parentId").setValue(curNode.attributes.permission.id);
					window.show();
				}else{
					Ext.Msg.alert("信息","请选择一节点");
				}
			}
			},{
				text: "修改权限",
				iconCls:'icon-edit',
				handler: function(){
				var curNode=tree.getSelectionModel().getSelectedNode();
					if(curNode){
						window.show();
						form.getForm().setValues(curNode.attributes.permission);
					}else{
						Ext.Msg.alert("信息","请选择一节点");
					}
				}
			},{
				text: "删除",
				iconCls:'icon-del',
				handler: function(){
					var curNode=tree.getSelectionModel().getSelectedNode();
					if(curNode){
						Ext.Msg.confirm("提示:","确定删除",function(btn){
							if(btn=='yes'){
								ajax.request({
					 				url:Uec3.App.Constants.homeUrl+'/permission/base!delete.do',
					 				params:{id:curNode.attributes.permission.id},
					 				success:function(response,options){
						 				var result = Ext.decode(response.responseText);
						 				if(result.success="OK"){
						 					curNode.remove(true);
						 				}
					 				}
								});
							}
						});
					}else{
						Ext.Msg.alert("信息","请修改再保存!");
					}
				}
		}],
        listeners: {
         	'remove':function(tree,parent,node){
         		if(!parent.hasChildNodes()){
         			parent.leaf=true;
					parent.select();
         		}
         	},
         	'append':function(tree,parent,node){
         		
         	},
         	'beforemovenode':function(tree,node,oldParent,newParent,index){
         		Ext.MessageBox.confirm("警告","确定移动",function(btn){
         			if(btn=='yes'){
         				ajax.request({
			 				url:Uec3.App.Constants.homeUrl+'/permission/base!updateParent.do',
			 				params:{
			 				id:node.attributes.permission.id,
			 				oldParentId:oldParent.attributes.permission.id,
			 				newParentId:newParent.attributes.permission.id},
			 				success:function(response,options){
				 				var result = Ext.decode(response.responseText);
				 				if(result.success="OK"){
				 					
				 				}else{
				 					Ext.MessageBox.alert("信息",result.success);
				 					tree.getLoader().load();
				 				}
			 				}
						});
         			}else{
         				return false;
         			}
         		});
         	},
         	'movenode':function(tree,node,oldParent,newParent,index){
         		
         	}
        }
    });
   
    module.objsCollection.add("tree",tree);
    new Ext.tree.TreeSorter(tree, {
	    folderSort: true,
	    dir: "asc",
	    //property:'sequence'
	   sortType: function(node) {
	        // sort by a custom, typed attribute:
	        return node.attributes.sequence;
	   }
	});
    var form=new Ext.FormPanel({
  		  height:60,
	      layout:'form',
	      border:true,
	      labelWidth:60,
	      layoutConfig: 
	         {     
	           padding:'5 5 5 5',
			   align:'middle' 
	      }, 
		items:[
			{
				xtype:'hidden',
				name : 'id'
			},
			{
				xtype:'hidden',
				name : 'parentId'
			},
			{
				xtype:'hidden',
				name : 'isGroup'
			},
			{
				fieldLabel : '权限名称',
				name : 'name',
				xtype:'textfield',
				width : 200
			},{
				fieldLabel : '权限全称',
				name : 'nameFullPath',
				xtype:'textfield',
				width : 200
			},{
				fieldLabel : '次序',
				name : 'sequence',
				maxLength:5,
				xtype:'numberfield',
				
				width : 200
			},{
            layout:"hbox",
            border:false,
            layoutConfig: {     
	           padding:'100 0 0 0',
			   align:'middle',
			   pack:'center'
	      	},
	      	items:[	            
	      	new Ext.Button({
            	text:"保存",
            	width:60,
            	iconCls:'icon-save',
            	handler:function(){
            		var curNode=tree.getSelectionModel().getSelectedNode();
            		var per=form.getForm().getValues();
            		var permissionJson=Ext.encode(encodeFilte(per));
					ajax.request({
		 				url:Uec3.App.Constants.homeUrl+'/permission/base!save.do',
		 				params:{permissionJson:permissionJson},
		 				success:function(response,options){
			 				var result = Ext.decode(response.responseText);
			 				if(result.success=="OK"){
			 					if(curNode.isLeaf()){
									curNode.leaf=false;
								}
								window.hide();
				 				var permission=result.permission;
								if(!per.id){
				 					var newNode=new Ext.tree.TreeNode({id:permission.id,text:permission.name});
				 					newNode.attributes.permission=permission;
									curNode.appendChild(newNode);
									curNode.expand();
									newNode.select();
								}else{
									curNode.setText(per.name);
									curNode.attributes.permission=permission;
								}
			 				}else{
			 					Ext.Msg.alert("信息",result.success);
			 				}
		 				}
					});
					
            	}
            }),
            new Ext.Button({
            	text:'关闭',
            	width:60,
            	iconCls:'icon-close',
            	handler:function(){
            		window.hide();
            	}
            })
            ]
	}]
    });
  
    var window=new Ext.Window({
    		layout:'fit',
		    width:300,
		    closeAction:'hide',
		    height:300,
		    title:"权限",
			resizable :true,
			manager:Uec3.App.getDesktop().windows,
			shadow : true,
			modal :true,
		    closable:true,
		    bodyStyle:'padding:5 5 5 5',
		    animCollapse:true,
			items:[form]
    });
   

	var contextMenu = new Ext.menu.Menu({
			items : [{
						text : '垂直平铺',
						handler : function(e){
							alert(e);
						}
					}]
		});
    
    module.objsCollection.add('contextMenu',contextMenu);
    
    //var treeEdit=new Ext.tree.TreeEditor(tree,{allowBlank: false});
 
	
	var panel=new Ext.Panel({
	    layout: 'column',
	    items: [tree]
	});
	module.objsCollection.add("panel",panel);
	return module;
	},
	initEvent:function(params){
		var module=this;
		var win=module.getObjByKey("win");
		var contextMenu=module.getObjByKey("contextMenu");
		win.on('show',function(){
		tree.el.on('contextmenu',function(e){
			//console.debug(contextMenu);
			//console.debug(e);
			e.stopEvent();
	         if(!contextMenu.el){
	            contextMenu.render();
	         }
	         var xy = e.getXY();
	         contextMenu.showAt(xy);
	    });
	},module,{single:true});
		var tree=module.getObjByKey("tree");
		//tree.expandAll();

	},
   createWindow : function(params){
   	  var windowId=this.id;
   	  if(params&&params.winId){
   	  	windowId+="_"+params.winId;
   	  }
      var desktop = Uec3.App.getDesktop();   
      if(params&&params.noCreate==true){
      	this.initPanel(params);
      	return false;
      }
      var win = desktop.getWindowByUecId(windowId);
      if(!win){ 
      	 var module=this;
      	 module.initPanel(params);
         win = desktop.createWindow({
            uecId: windowId,
            width: 700,
            height: 400,
            title:'权限管理',
            iconCls: 'acc-icon-saleInvoice',
            shim: false,
            moduleObj:module,
            constrainHeader: true,
            maximizable: true,
            layout: "fit", 	
            items:[module.getObjByKey('panel')],
            listeners:{
            	'resize':function(win,width,height){
            		var treePanel=this.items.get(0).items.first();
            		//var treePanel=this.items.get(0).items.last();
            		var winMinWidth=700;
            		var winMinHeight=400;
            		if(width<winMinWidth||height<winMinHeight){
            			win.setWidth(winMinWidth);
            			win.setHeight(winMinHeight);           			
            		}else{
            			 treePanel.setWidth(width-10);
         				 treePanel.setHeight(height-30);
            		}
            	}
            	
            }
         });
         module.objsCollection.add("win",win);
         module.initEvent();
      } 
      win.show();
   }
});
